#!/usr/bin/env python
# -*- coding: utf-8 -*

from clutter import Group, Rectangle 
import alwaysontop

spacing = 5

class LBox(Group):
    def __init__(self, orientation="vertical"):
        self.orientation = orientation
        Group.__init__(self)
        self.fill = 0
        self._build_background()

    def add(self, actor):
        super(Group, self).add(actor)
        if self.orientation == "vertical": 
            actor.set_position(0,self.fill) 
            self.fill += actor.get_height() + spacing
        elif self.orientation == "horizontal":
            actor.set_position(self.fill, 0)
            self.fill += actor.get_width() + spacing

    def _build_background(self):
        self.back = r = Rectangle()
        r.set_size(self.get_width(), self.get_height())
        r.set_color(alwaysontop.box_back_color)
        super(Group, self).add(r)
        r.lower_bottom()

    def get_width(self):
        width = 0
        for child in self.get_children():
            if self.orientation == "horizontal":
                width += spacing + child.get_width()
            elif self.orientation == "vertical":
                width = max(child.get_width(), width)
        return width

    def get_height(self):
        height = 0
        for child in self.get_children():
            if self.orientation == "vertical":
                height += spacing + child.get_height()
            else:
                height = max(child.get_height(), height)
        return height
